﻿using System.Collections.Generic;

namespace TXLooker.Common.EventHandling
{
    public delegate void EventSubscriber();

    public class EventSink
    {
        private IDictionary<string, IList<EventSubscriber>> subscribers;

        public EventSink()
        {
            subscribers = new Dictionary<string, IList<EventSubscriber>>();
        }

        public void Subscribe(string eventName, EventSubscriber subscriberHandler)
        {
            if(!subscribers.ContainsKey(eventName))
            {
                subscribers.Add(eventName, new List<EventSubscriber>());
            }

            subscribers[eventName].Add(subscriberHandler);
        }

        public void Publish(string eventName)
        {
            if (!subscribers.ContainsKey(eventName)) return;

            foreach(EventSubscriber subscriber in subscribers[eventName])
            {
                subscriber();
            }
        }
    }
}
